package com.xcw.bos.web.action;

import java.io.IOException;
import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.xcw.bos.domain.Staff;
import com.xcw.bos.page.PageBean;
import com.xcw.bos.web.action.base.BaseAction;

/**
 * 取派员操作Action
 * @author xcw
 *
 */
@Controller
@Scope("prototype")
public class StaffAction extends BaseAction<Staff>{
	//保存方法
	public String save(){
		staffService.save(model);
		return "staffList";
	}
	
	//分页查询，返回json数据
	public String pageQuery() throws IOException{
		
		//按照取派员的id进行降序
		pageBean.getDetachedCriteria().addOrder(Order.desc("id"));
		
		
		staffService.pageQuery(pageBean);
		
		//排除不需要序列化的属性
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(new String[]{"page","pageSize","detachedCriteria","decidedZones"});
		
		JSONObject jsonObject = JSONObject.fromObject(pageBean,jc);
		String json = jsonObject.toString();
		
		ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
		ServletActionContext.getResponse().getWriter().print(json);
		return NONE;
	}
	
	private String ids;//接收id字符串
	
	//取派员作废
	public String delete(){
		staffService.delete(ids);
		return "staffList";
	}
	
	//修改取派员信息
	public String update(){
		//原始数据查询
		Staff staff = staffService.findById(model.getId());
		
		//修改
		staff.setName(model.getName());
		staff.setStation(model.getStation());
		staff.setTelephone(model.getTelephone());
		staff.setStandard(model.getStandard());
		staff.setHaspda(model.getHaspda());
		
		staffService.update(staff);
		return "staffList";
	}
	
	//查询为作废的取派员
	public String listajax(){
		DetachedCriteria dc = DetachedCriteria.forClass(Staff.class);
		//查询deltag == 0
		dc.add(Restrictions.eq("deltag", "0"));
		//未分配定区的取派员
		dc.add(Restrictions.isEmpty("decidedZones"));
		dc.addOrder(Order.desc("id"));
		List<Staff> list = staffService.findByCondition(dc);
		
		String[] excludes = new String[]{"telephone","haspda","deltag","station","standard","decidedZones"};
		this.writeList2Json(excludes, list);
		return NONE;
	}
	public String getIds(){
		return ids;
	}
	
	public void setIds(String ids){
		this.ids = ids;
	}

}
